package thread;

class Joiner extends Thread {

	private int sleepTime;
	
	public Joiner(String name, int sleepTime) {
		super(name);
		this.sleepTime = sleepTime;
		start();
		System.out.println(getName() + " has started");
	}
	
	public void run() {
		
		try {
			System.out.println(getName() + " has begun to sleep for " + sleepTime + "ms");
			sleep(sleepTime);
		} catch (InterruptedException e) {
			System.out.println(getName() + " was interrupted. "
					+ "isInterrupted(): " + isInterrupted());
			return;
		}
		System.out.println(getName() + " has awakened");
	}
}

class Joined extends Thread {
	private Joiner joiner;
	public Joined(String name, Joiner joiner) {
		super(name);
		this.joiner = joiner;
		start();
		System.out.println(getName() + " has started");
	}
	
	public void run() {
		try {
			System.out.println(joiner.getName() + " has joined " + this.getName());
			joiner.join();
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
		System.out.println(getName() + " join completed");
	}
}

public class Joining {
	public static void main(String[] args) {
		Joiner sleepy = new Joiner("Sleepy", 3000);
		Joiner grumpy = new Joiner("Grumpy", 3000);

		Joined dopey = new Joined("Dopey", sleepy);
		Joined doc = new Joined("Doc", grumpy);

		try {
			Thread.sleep(1500);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
		
		grumpy.interrupt();
	}
}
